﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using HtmlAgilityPack;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;

using WowheadScraper;
using BattleNet;

namespace PlayForMe
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string _realmName;
        public MainWindow()
        {
            InitializeComponent();

            SetupGUI();






        }
        public void SetupGUI()
        {

            // Populate classes
            string[] classes = new string[10];
            classes[0]="Druid";
            classes[1]="Death Knight";
            classes[2]="Hunter";
            classes[3]="Mage";
            classes[4]="Paladin";
            classes[5]="Priest";
            classes[6]="Rogue";
            classes[7]="Shaman";
            classes[8]="Warlock";
            classes[9] = "Warrior";
            foreach(string classs in classes)
                ClassListbox.Items.Add(classs);

            
        }
        public string GetURL(string classs, string spec, string itemType, string faction)
        {
            string url=null;
            URLDesigner urlDesigner = new URLDesigner();

            urlDesigner.SetItemType(itemType); // Must call SetItemType followed by SetItemSubtype before any other method
            urlDesigner.SetItemSubtype("Two-Handed");
            urlDesigner.SetPresetClass(classs, spec);
            urlDesigner.SetReputationType(faction);
            url=urlDesigner.GetURL();
            UrlTextBox.Text = url;
            return url;
        }


        private void RegionCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string value = ((ComboBox)e.OriginalSource).SelectedValue.ToString();
            string[] realmlist=null;
            switch (value[value.Length-1])
            {
                case 'U':
                    // load EU data into realm combobox
                    realmlist = WoWDataStore.Realmlist_EU();                    
                    break;
                case 'S':
                    //load US data into ream combobox
                    realmlist = WoWDataStore.Realmlist_US();  
                    break;
            }

            foreach (string realm in realmlist)
                ReamCombobox.Items.Add(realm);
        }


        private void RealmCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string value = ((ComboBox)e.OriginalSource).SelectedValue.ToString();
            _realmName = WoWDataStore.URLFriendlyRealmName(value);
        }

        private void ClassListbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SpecListbox.Items.Clear();
            string[] spec=new string[4];
            switch (ClassListbox.SelectedItem.ToString())
            {
                case "Druid":
                    
                    spec[0] = "Balance (DPS)";
                    spec[1] = "Feral (DPS)";
                    spec[2] = "Feral (Tank)";
                    spec[3] = "Restoration (Healing)";
                    break;
                case "Death Knight":
                    spec[0] = "Blood (Tank)";
                    spec[1] = "Frost (DPS)";
                    spec[2] = "Unholy (DPS)";
                    break;
                case "Hunter":
                    spec[0] = "Beast Mastery";
                    spec[1] = "Marksmanship";
                    spec[2] = "Survival";
                    break;
                case "Mage":
                    spec[0] = "Arcane";
                    spec[1] = "Fire";
                    spec[2] = "Frost";
                    break;
                case "Paladin":
                    spec[0] = "Holy (Healing)";
                    spec[1] = "Protection (Tank)";
                    spec[2] = "Retribution (DPS)";
                    break;
                case "Priest":
                    spec[0] = "Discipline (Healing)";
                    spec[1] = "Holy (Healing)";
                    spec[2] = "Shadow Magic (DPS)";
                    break;
                case "Rogue":
                    spec[0] = "Assassination";
                    spec[1] = "Combat";
                    spec[2] = "Subtely";
                    break;
                case "Shaman":
                    spec[0] = "Elemental Combat (DPS)";
                    spec[1] = "Enhancement (DPS)";
                    spec[2] = "Restoration (Healing)";
                    break;
                case "Warlock":
                    spec[0] = "Affliction";
                    spec[1] = "Demonology";
                    spec[2] = "Destruction";
                    break;
                case "Warrior":
                    spec[0] = "Arms (DPS)";
                    spec[1] = "Fury (DPS)";
                    spec[2] = "Protection (Tank)";
                    break;
                default:
                    throw new Exception("Class/spec combination not valid: " + ClassListbox.SelectedItem.ToString());
            }
            foreach (string sp in spec)
                SpecListbox.Items.Add(sp);
        }

        private void LoadCharButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                List<string> cataReps = new List<string>();

                cataReps.Add("Baradin's Wardens");
                cataReps.Add("Guardians of Hyjal");
                cataReps.Add("Hellscream's Reach");
                cataReps.Add("Ramkahen");
                cataReps.Add("The Earthen Ring");
                cataReps.Add("Therazane");
                cataReps.Add("Wildhammer Clan");

                BattleNetScraper BNS = new BattleNetScraper();
                string classs = BNS.GetClass(this.CharcterNameTextbox.Text.ToString(), _realmName);
                List<BattleNetScraper.FactionDetail> fdl = BNS.GetReputation(this.CharcterNameTextbox.Text.ToString(),_realmName);
                this.ClassListbox.SelectedItem = classs;


                foreach (string rep in cataReps)
                {
                    string URL = GetURL(classs, "Feral (Tank)", "Weapons", rep);
                    WowheadScraper.PageScraper page = new PageScraper(URL);

                    List<PageScraper.WowheadItem> ListOfGear = page.ParseWowheadPageForResults();

                    foreach (PageScraper.WowheadItem item in ListOfGear)
                    {
                        GearListbox.Items.Add(item.Name);
                    }
                    foreach (BattleNetScraper.FactionDetail faction in fdl)
                    {
                        RepListbox.Items.Add(faction.Name);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
